package com.pw.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pw.web.bean.C3;
import com.pw.web.bean.Spu;
import com.pw.web.bean.Tm;
import com.pw.web.service.ProductService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: linux_future
 * @since: 2022/3/29
 **/
@RestController
public class ProductController {
    @Autowired
    ProductService productService;

    @GetMapping("/gmv")
    public String product(@RequestParam(value = "date", defaultValue = "0") int date) {
        System.out.println("-------------------测试------------");
        if (date == 0) {
            date = Integer.parseInt(DateFormatUtils.format(new Date(), "yyyyMMDD"));
        }
        BigDecimal gmv = productService.getGMV(date);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("status", 200);
        map.put("msg", "success");
        map.put("data", gmv);


        return JSON.toJSONString(map);
    }

    @GetMapping("/gmTrademark")
    public String product2(@RequestParam(value = "date", defaultValue = "0") int date
            , @RequestParam(value = "limit", defaultValue = "10") int limit) {
        System.out.println("-------------------测试-getProductByTrademark-----------");
        if (date == 0) {
            date = Integer.parseInt(DateFormatUtils.format(new Date(), "yyyyMMDD"));
        }
        List<Tm> tmList = productService.getProductByTrademark(date, limit);
        System.out.println(tmList.toString());

        JSONObject data = new JSONObject();
        JSONArray categories = new JSONArray();
        JSONArray series = new JSONArray();
        for (Tm tm : tmList) {
            categories.add(tm.getTm_name());
        }
        data.put("categories", categories);
        JSONObject obj = new JSONObject();
        obj.put("name", "商品品牌");
        JSONArray data1 = new JSONArray();
        for (Tm tm : tmList) {
            data1.add(tm.getOrder_amount());
        }
        obj.put("data", data1);
        series.add(obj);
        data.put("series", series);

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("status", 200);
        map.put("msg", "success");
        map.put("data", data);

        return JSON.toJSONString(map);
    }

    @GetMapping("/c3")
    public String ProductByC3(@RequestParam(value = "date", defaultValue = "0") int date) {
        System.out.println("===ProductByC3-----------" + date);
        if (date == 0) {
            date = Integer.parseInt(DateFormatUtils.format(new Date(), "yyyyMMDD"));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("status", 200);
        map.put("msg", "success");
        List<C3> c3List = productService.getProductByC3(date);
        System.out.println(c3List.toString());
        JSONArray data = new JSONArray();
        for (C3 c3 : c3List) {
            JSONObject obj = new JSONObject();
            obj.put("name", c3.getCategory3_name());
            obj.put("value", c3.getOrder_amount());
            System.out.println(obj.toJSONString());
            data.add(obj);
        }
        System.out.println("data: " + data.toJSONString());
        map.put("data", data);
        return JSON.toJSONString(map);
    }

    @GetMapping("/spu")
    public String ProductBySpu(@RequestParam(value = "date", defaultValue = "0") int date) {
        System.out.println("===ProductBySpu-----------" + date);
        if (date == 0) {
            date = Integer.parseInt(DateFormatUtils.format(new Date(), "yyyyMMDD"));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("status", 200);
        map.put("msg", "success");

        List<Spu> spuList = productService.getProductBySpu(date);
        JSONObject data = new JSONObject();
        JSONArray columns = new JSONArray();
        JSONObject c1 = new JSONObject();
        c1.put("id", "spu 名");
        c1.put("name", "spu");
        columns.add(c1);

        JSONObject c2 = new JSONObject();
        c2.put("name", "销售额");
        c2.put("id", "order_amount");
        columns.add(c2);
        data.put("columns", columns);
        JSONArray rows = new JSONArray();
        for (Spu spu : spuList) {
            JSONObject row = new JSONObject();
            row.put("spu", spu.getSpu_name());
            row.put("order_amount", spu.getOrder_amount());
            rows.add(row);
        }
        data.put("rows", rows);
        map.put("data", data);
        return JSON.toJSONString(map);
    }
}
